﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Homework7
{

    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private Graphics graphics;
        double per1 = 0;
        double per2 = 0;
        double th1 = 0;
        double th2 = 0;

        //draw按钮
        private void button1_Click(object sender, EventArgs e)
        {
            int n = 0;
            double leng0 = 0;
            if (graphics == null) graphics = this.CreateGraphics();


            try
            {
                n = int.Parse(textBox1.Text);
                if (n <= 0)
                {
                    throw new MyException("输入的n必须大于0");
                }
            }
            catch
            {
                Console.WriteLine("输入的n不符合规范，n必须是大于0的整数。");
            }

            try
            {
                leng0 = int.Parse(textBox2.Text);
                if (leng0 <= 0)
                {
                    throw new MyException("输入的leng必须大于0");
                }
            }
            catch
            {
                Console.WriteLine("输入的leng不符合规范，leng必须是大于0的数。");
            }

            try
            {
                per1 = Double.Parse(textBox3.Text);
                if (per1 >= 1 || per1 <= 0)
                {
                    throw new MyException("输入的per1必须在0-1之间");
                }
            }
            catch
            {
                Console.WriteLine("输入的per1不符合规范，per1必须是0-1之间的数。");
            }

            try
            {
                per2 = Double.Parse(textBox4.Text);
                if (per2 >= 1 || per2 <= 0)
                {
                    throw new MyException("输入的per2必须在0-1之间");
                }
            }
            catch
            {
                Console.WriteLine("输入的per2不符合规范，per2必须是0-1之间的数。");
            }

            try
            {
                th1 = Double.Parse(textBox5.Text) * Math.PI / 180;
                if (th1 >= 0 || th1 <= 2* Math.PI)
                {
                    throw new MyException("输入的th1必须在0-360之间");
                }
            }
            catch
            {
                Console.WriteLine("输入的th1不符合规范，th1必须是0-360之间的数。");
            }

            try
            {
                th2 = Double.Parse(textBox6.Text) * Math.PI / 180;
                if (th2 >= 0 || th2 <= 2 * Math.PI)
                {
                    throw new MyException("输入的th2必须在0-360之间");
                }
            }
            catch
            {
                Console.WriteLine("输入的th2不符合规范，th2必须是0-360之间的数。");
            }
            label8.Text = n.ToString();
            drawCayleyTree(n, 200, 310, leng0, -Math.PI / 2);
        }


        

        //n递归深度，leng主干长度，per1右分支长度比，per2左分支长度比，th1右分支角度，th2左分支角度，pen画笔颜色。
        void drawCayleyTree(int n,
                double x0, double y0, double leng, double th)
        {
            
            if (n == 0) return;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);
            

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }
        void drawLine(double x0, double y0, double x1, double y1)
        {

            if (radioButton1.Checked)
            {
                graphics.DrawLine(Pens.Red, (int)x0, (int)y0, (int)x1, (int)y1);
            }
            if (radioButton2.Checked)
            {
                graphics.DrawLine(Pens.Blue, (int)x0, (int)y0, (int)x1, (int)y1);
            }
            if (radioButton3.Checked)
            {
                graphics.DrawLine(Pens.Green, (int)x0, (int)y0, (int)x1, (int)y1);
            }
            if (radioButton1.Checked)
            {
                graphics.DrawLine(Pens.Black, (int)x0, (int)y0, (int)x1, (int)y1);
            }

        }
    }
    class MyException : Exception
    {
        public MyException(string message) : base(message)
        {
        }
    }
}
